package com.supertange.miniapp.wepet.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 时间操作工具类
 * 
 */
public class DateUtil {

	public static Logger logger = LoggerFactory.getLogger(DateUtil.class);

	public static final String SIMPLE_DATE_PATTERN = "yyyy-MM-dd";

	public static final String BILL_DATE_PATTERN = "yyyyMMdd";

	public static final String FULL_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

	public static final String FILE_DATE_PATTERN = "yyyyMMddHHmmss";

	/**
	 * 获取当前时间字符串
	 * @param format
	 * @return
	 */
	public static String getCurrentDate(String format) {
		return formatDate(new Date(), format);
	}

	/**
	 * 将时间转为字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatDate(Date date, String format) {
		if (date == null){
			return "";
		}
		DateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	/**
	 * 解析字符串为日期
	 * @param time
	 * @param format
	 * @return
	 */
	public static Date parse(String time, String format) throws Exception {
		if (StringUtils.isBlank(time)){
			return null;
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = simpleDateFormat.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 获得某一个月有多少天
	 * @param year
	 * @param month
	 * @return
	 */
	public static int getConcreateDaysOfMonth(int year, int month) {
		Calendar cal = new GregorianCalendar(year, month - 1 , 1);
		cal.add(Calendar.DATE, -1);
		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获得某一天是星期几
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static int getConcreateDaysOfWeek(int year, int month, int day) {
		Calendar calendar = new GregorianCalendar(year, month - 1, day - 1);
		int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		return dayOfWeek;
	}


}
